package org.redr.widgetGUI.settings;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class RedRSettings extends XMLSetting{
	
	private Document d;
	private Element notes;
	private Element history;
	private Element objects;
	private Element widgets;
	private Element connections;
	private Element variables;
	
	private SessionSettings notesSettings;
	private SessionSettings historySettings;
	
	public RedRSettings(Document dd) throws Exception{
		d = dd;
		e = dd.getElementById("WidgetSettings");
		notes = dd.getElementById("notes");
		if (notes == null) throw new Exception("Poorly formed xml");
		notesSettings = new SessionSettings(notes);
		
		history = dd.getElementById("history");
		if (history == null) throw new Exception("Poorly formed xml");
		historySettings = new SessionSettings(history);
		
		objects = dd.getElementById("objects");
		if (objects == null) throw new Exception("Poorly formed xml");
		widgets = dd.getElementById("widgets");
		if (widgets == null) throw new Exception("Poorly formed xml");
		connections = dd.getElementById("connections");
		if (connections == null) throw new Exception("Poorly formed xml");
		variables = dd.getElementById("variables");
		if (variables == null) throw new Exception("Poorly formed xml");
	}
	
	public RedRSettings() throws ParserConfigurationException{
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
 
		// root elements
		d = docBuilder.newDocument();
		Element doc = d.createElement("document"); // this is the main node, all others attached to this.
		
		d.appendChild(doc);
		
		e	=	d.createElement("WidgetSettings");
		notes = d.createElement("notes");
		notesSettings = new SessionSettings(notes);
		history = d.createElement("history");
		historySettings = new SessionSettings(history);
		objects = d.createElement("objects");
		widgets = d.createElement("widgets");
		connections = d.createElement("connections");
		variables = d.createElement("variables");
		
		doc.appendChild(e);
		doc.appendChild(notes);
		doc.appendChild(history);
		doc.appendChild(objects);
		doc.appendChild(widgets);
		doc.appendChild(connections);
		doc.appendChild(variables);
		
		
	}
	
	public SessionSettings notesSettings(){
		return notesSettings;
	}
	
	public SessionSettings historySettings(){
		return historySettings;
	}
	
	/**
	 * Create a new node for a Red-R Widget.
	 * @param id
	 * @return new WidgetSettings
	 */
	public WidgetSettings newRedRWidget(String id){
		Element newE = e.getOwnerDocument().createElement("RedRWidget");
		newE.setAttribute("id", id);
		e.appendChild(newE);
		return new WidgetSettings(newE);
	}
	
	public WidgetSettings getSettings(String id){
		Element newE = e.getOwnerDocument().createElement("QTWidgetSetting");
		newE.setAttribute("id", id);
		e.appendChild(newE);
		return new WidgetSettings(newE);
	}
	
	public void saveSettings(File settingsFile) throws TransformerFactoryConfigurationError, TransformerException {
		Transformer transformer = TransformerFactory.newInstance().newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");

		//initialize StreamResult with File object to save to file
		StreamResult result = new StreamResult(settingsFile);
		DOMSource source = new DOMSource(d);
		transformer.transform(source, result);

		String xmlString = result.getWriter().toString();
		System.out.println(xmlString);
	}
	
}
